package org.ax.service.plan.imple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ax.mapper.plan.WorkMapper;
import org.ax.pojo.device.Device;
import org.ax.pojo.plan.Work;
import org.ax.service.device.DeviceService;
import org.ax.service.plan.ProductService;
import org.ax.service.plan.WorkService;
import org.ax.service.process.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 编程人:赵浩鑫
 * 时间:2021-02-25-14:45
 */
@Service
public class IWorkService extends ServiceImpl<WorkMapper, Work> implements WorkService {
    @Autowired
    ProductService productService;
    @Autowired
    ProcessService processService;
    @Autowired
    DeviceService deviceService;
    @Override
    public Page<Work> searchWorkPage(Page page, String productId, String processId) {
        QueryWrapper<Work> queryWrapper=new QueryWrapper<Work>();
        queryWrapper.eq(!StringUtils.isBlank(processId), "product_id",productId);
        queryWrapper.eq(!StringUtils.isBlank(processId), "process_id",processId);
        Page page1 = getBaseMapper().selectPage(page, queryWrapper);
        List<Work> workList=(List<Work>)page1.getRecords();
        for (Work work : workList) {
            work.setProduct(productService.toProductId(work.getProductId()));
            work.setProcess(processService.toProcessId(work.getProcessId()));
            work.setDevice(deviceService.toDeviceId(work.getDeviceId()));
        }
        page1.setRecords(workList);
        return page1;
    }

    @Override
    public boolean addWork(Work work) {
        return getBaseMapper().insert(work)>0;
    }

    @Override
    public boolean updWork(Work work) {
        return getBaseMapper().updateById(work)>0;
    }

    @Override
    public boolean deletesWork(String[] ids) {
        return getBaseMapper().deleteBatchIds(Arrays.asList(ids))>0;
    }

    @Override
    public Work toWorkId(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<Work> selectWorks() {
        return getBaseMapper().selectList(null);
    }
}
